<?php

namespace app\admin\controller;

use think\Db;
use think\facade\Env;
use think\facade\Request;

class Slide extends Common
{
    public function initialize()
    {
        parent::initialize();
    }
    
    //广告列表
    public function index()
    {
        if (Request::isAjax()) {
            $key = input('post.key');
            $this->assign('testkey', $key);
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = db('slide')
                ->where('title', 'like', "%" . $key . "%")
                ->order('sort')
                ->paginate(['list_rows' => $pageSize, 'page' => $page])
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total']];
        }
        return $this->fetch();
    }
    
    public function add()
    {
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');
            $data['create_time'] = time();
            db('slide')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '操作成功!';
            cache('adList', null);
            $result['url'] = url('index');
            return $result;
        } else {
            $this->assign('title', lang('add') . lang('ad'));
            $this->assign('info', 'null');
            $rt = template_file('slide');
            $this->assign('template', $rt);
            return $this->fetch('edit');
        }
    }
    
    public function edit()
    {
        if (Request::isAjax()) {
            $data = Request::except('file');
            db('slide')->update($data);
            $result['code'] = 1;
            $result['msg'] = '操作成功!';
            cache('adList', null);
            $result['url'] = url('index');
            return $result;
        } else {
            $id = input('id');
            $Info = db('slide')->where(['id' => $id])->find();
            $rt = template_file('slide');
            $this->assign('template', $rt);
            $this->assign('info', $Info);
            $this->assign('title', lang('edit') . lang('ad'));
            return $this->fetch('edit');
        }
    }
    
    //设置广告状态
    public function editState()
    {
        $id = input('post.id');
        $status = input('post.status');
        if (db('slide')->where('id=' . $id)->update(['status' => $status]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }
    
    public function adOrder()
    {
        $data = input('post.');
        if (db('slide')->update($data) !== false) {
            cache('adList', null);
            return ['msg' => '操作成功！', 'url' => url('index'), 'code' => 1];
        } else {
            return ['code' => 0, 'msg' => '操作失败！'];
        }
    }
    
    public function del()
    {
        db('slide')->where(['id' => input('id')])->delete();
        cache('adList', null);
        return ['code' => 1, 'msg' => '删除成功！'];
    }
    
    public function delall()
    {
        $map[] = ['id', 'in', input('param.ids/a')];
        db('slide')->where($map)->delete();
        cache('adList', null);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
    
    /***************************位置*****************************/
    //位置
    public function picList()
    {
        if (Request::isAjax()) {
            $key = input('key');
            $sid = input('id');
            $this->assign('testkey', $key);
            $list = db('slide_data')->where('sid', $sid)->where('title', 'like', "%" . $key . "%")->order('sort')->select();
            foreach ($list as $k => $v) {
                $list[$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
            }
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list];
        }
        
        $id = input('id');
        $this->assign('sid', $id);
        return $this->fetch();
    }
    
    public function picOrder()
    {
        $data = input('post.');
        if (db('slide_data')->update($data) !== false) {
            return ['msg' => '操作成功！', 'url' => url('type'), 'code' => 1];
        } else {
            return ['code' => 0, 'msg' => '操作失败！'];
        }
    }
    
    public function picAdd()
    {
        if (Request::isAjax()) {
            $data = input('post.');
            unset($data['file']);
            $data['create_time'] = time();
            db('slide_data')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '保存成功!';
            $result['url'] = url('piclist') . '?id=' . $data['sid'];
            return $result;
        } else {
            $this->assign('title', lang('add') . lang('ad') . '位');
            $this->assign('info', 'null');
            return $this->fetch('picedit');
        }
    }
    
    public function picEdit()
    {
        if (Request::isAjax()) {
            $data = input('post.');
            unset($data['file']);
            db('slide_data')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('piclist') . '?id=' . $data['sid'];
            return $result;
        } else {
            $id = input('param.id');
            $info = db('slide_data')->where('id', $id)->find();
            $this->assign('title', lang('edit') . lang('ad') . '位');
            $this->assign('info', $info);
            return $this->fetch('picedit');
        }
    }
    
    
    //设置图片状态
    public function editPicState()
    {
        $id = input('post.id');
        $status = input('post.status');
        if (db('slide_data')->where('id=' . $id)->update(['status' => $status]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }
    
    public function delPic()
    {
        $map['id'] = input('param.id');
        db('slide_data')->where($map)->delete();//删除广告位
        return ['code' => 1, 'msg' => '删除成功！'];
    }
    
    public function delAllPic()
    {
        $map[] = ['id', 'in', input('param.ids/a')];
        db('slide_data')->where($map)->delete();
        cache('adList', null);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
}